﻿using System;
using PM.BusinessLayer;


public partial class UserManagement_UserDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["UserID"] == null)
            {
                fmvUser.ChangeMode(System.Web.UI.WebControls.FormViewMode.Insert);
            }
            if(fmvUser.CurrentMode == System.Web.UI.WebControls.FormViewMode.Insert)
            {
                grvUserRole.Visible = false;
                ddlMissingRoles.Visible = false;
                btnAddToRole.Visible = false;
            }
        }

        if (Request.QueryString["UserID"] != null)
        {
            ddlMissingRoles.Visible = new UserController().IsAllUserInRoles(int.Parse(Request.QueryString["UserID"].ToString()));
            btnAddToRole.Visible = ddlMissingRoles.Visible;
        }
    }

    public void btnDeleteUser_Click(object sender, EventArgs e)
    {
        fmvUser.DeleteItem();
        Response.Redirect("~/UserManagement/Users.aspx");
    }

    public void btnInsert_Insert_Click(object sender, EventArgs e)
    {
        fmvUser.InsertItem(true);
        Response.Redirect("~/UserManagement/Users.aspx");
    }

    public void btnAddToRole_Click(object sender, EventArgs e)
    {
        new UserController().AddToRole((int)fmvUser.DataKey.Value, int.Parse(ddlMissingRoles.SelectedValue));
        
        grvUserRole.DataBind();
        ddlMissingRoles.DataBind();
        ddlMissingRoles.Visible = ddlMissingRoles.Items.Count > 0;
        btnAddToRole.Visible = ddlMissingRoles.Visible;
        Response.Redirect(Request.RawUrl);
    }

    protected void grvUserRole_RowDeleted(object sender, System.Web.UI.WebControls.GridViewDeletedEventArgs e)
    {
        ddlMissingRoles.DataBind();
        Response.Redirect(Request.RawUrl);
    }
}
